package com.eshop.modules.security.security;

import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import java.io.IOException;
import org.springframework.security.core.userdetails.UserDetails;
import com.eshop.modules.user.vo.OnlineUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import com.eshop.utils.StringUtils;
import io.jsonwebtoken.ExpiredJwtException;
import com.eshop.modules.security.service.OnlineUserService;
import com.eshop.utils.SpringContextHolder;
import com.eshop.modules.security.config.SecurityProperties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.FilterChain;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import org.slf4j.Logger;
import org.springframework.web.filter.GenericFilterBean;

public class TokenFilter extends GenericFilterBean
{
    private static final Logger log;
    private final TokenUtil tokenUtil;
    
    TokenFilter(final TokenUtil tokenUtil) {
        this.tokenUtil = tokenUtil;
    }
    
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
        final HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
        final String requestRri = httpServletRequest.getRequestURI();
        OnlineUser onlineUser = null;
        String authToken = "";
        try {
            final SecurityProperties properties = (SecurityProperties)SpringContextHolder.getBean((Class)SecurityProperties.class);
            final OnlineUserService onlineUserService = (OnlineUserService)SpringContextHolder.getBean((Class)OnlineUserService.class);
            authToken = this.tokenUtil.getToken(httpServletRequest);
            if (authToken == null) {
                filterChain.doFilter(httpServletRequest, servletResponse);
                return;
            }
            onlineUser = onlineUserService.getOne(properties.getOnlineKey() + authToken);
        }
        catch (ExpiredJwtException e) {
            TokenFilter.log.error(e.getMessage());
        }
        final String username = StringUtils.isNotBlank((CharSequence)authToken) ? this.tokenUtil.getUsernameFromToken(authToken) : null;
        if (onlineUser != null && username != null && SecurityContextHolder.getContext().getAuthentication() == null && this.tokenUtil.validateToken(authToken)) {
            final UserDetails userDetails = this.tokenUtil.getUserDetails(authToken);
            final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
            SecurityContextHolder.getContext().setAuthentication((Authentication)authentication);
            TokenFilter.log.debug("set Authentication to security context for '{}', uri: {}", authentication.getName(), requestRri);
        }
        else {
            this.tokenUtil.removeToken(authToken);
            TokenFilter.log.debug("no valid JWT token found, uri: {}", requestRri);
        }
        filterChain.doFilter((ServletRequest)httpServletRequest, servletResponse);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)TokenFilter.class);
    }
}
